package com.bc66.tiger.bizsys.menucategories.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bc66.tiger.bizsys.menucategories.dto.MenuCategoriesBeanDto;
import com.bc66.tiger.bizsys.menucategories.exception.MenuCategoriesException;
import com.bc66.tiger.bizsys.menucategories.model.MenuCategoriesBean;
import com.bc66.tiger.bizsys.menucategories.service.IMenuCategoriesService;
import com.bc66.tiger.bizsys.menucategories.validate.IValidateForm;
import com.bc66.tiger.framework.base.BaseRestSpringController;
import com.bc66.tiger.framework.page.PageRequest;

@Controller
@RequestMapping("/menucategories")
public class MenuCategoriesController extends
		BaseRestSpringController<MenuCategoriesBean, java.lang.Long> {
	
	protected static final String DEFAULT_SORT_COLUMNS = null;
	
	@Autowired
	@Qualifier("menuCategoriesService")
	private IMenuCategoriesService _menuCategoriesService;
	
	@Autowired
	@Qualifier("validateForm")
	private IValidateForm<MenuCategoriesBeanDto> _validateForm;
	
	@Override
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response, MenuCategoriesBean model) {
		// TODO Auto-generated method stub
		
		return new ModelAndView("/bizsys/menucategories/index");
	}
	
	@RequestMapping(value="/create.json",method=RequestMethod.POST)
	public ModelAndView newCategories(HttpServletRequest request,
			HttpServletResponse response, MenuCategoriesBeanDto model)
			throws Exception {
		// TODO Auto-generated method stub
		
		ModelMap mmap=new ModelMap();
		
		MenuCategoriesBeanDto mcbd=(MenuCategoriesBeanDto)model;
		
		mcbd.setUserid("7777777777");
		
		mcbd.setShopid("7777777777");

		this.checkBean(mcbd,mmap);
		
		if("0".equals(String.valueOf(mmap.get("code")).trim())){
			
			_menuCategoriesService.newMenuCategories(mcbd,mmap);
			
		}
		
		return new ModelAndView("create.json",mmap);		
		
	}
	
	
	@RequestMapping(value="/{id}/edit.json")
	public ModelAndView editData(@PathVariable Long id) throws Exception {
		// TODO Auto-generated method stub
		
		ModelMap mmap=new ModelMap();
		
		_menuCategoriesService.selectOne(id, mmap);
		
		return new ModelAndView("edit.json",mmap);
		
		
	}
	
	
	
	/**
	 * 
	 * wait refactoring 
	 * 
	 * **/
	@RequestMapping("/data.json")
	public ModelAndView jsonlist(HttpServletRequest request,
			HttpServletResponse response){
		
		//test
		this.showRquestProperty(request);
		
		MenuCategoriesBean mcb=new MenuCategoriesBean();
		
		ModelMap mmap=new ModelMap();
		
		String sSearch="";
		try {

			sSearch=
				new String(
				(StringUtils.isEmpty(request.getParameter("sSearch"))?"":request.getParameter("sSearch"))
						.getBytes("iso8859-1"),"UTF-8");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		int pagesize=StringUtils.isNumeric((request.getParameter("iDisplayLength")))?new Integer(request.getParameter("iDisplayLength")).intValue():10;
		
		int startsize=StringUtils.isNumeric((request.getParameter("iDisplayStart")))?new Integer(request.getParameter("iDisplayStart")).intValue():0;
		
		mcb.setEgname(sSearch);
		
		mcb.setMenuname(sSearch);
		
		mcb.setPinyin(sSearch);
		
		PageRequest pr=newPageRequest(request,DEFAULT_SORT_COLUMNS);
		
		pr.setPageSize(pagesize);
		
		pr.setPageNumber((startsize/pagesize)+1);
		
		pr.setFilters(mcb);
		
		this._menuCategoriesService.findByPageRequest(pr,mmap);
		
		return new ModelAndView("json_source.json",mmap);
	}
	
	
	@RequestMapping(value="/{id}/del.json",method=RequestMethod.POST)
	public ModelAndView deleteOne(@PathVariable Long id) {
		// TODO Auto-generated method stub
		
		ModelMap mmap=new ModelMap();
		
		_menuCategoriesService.deleteOne(id, mmap);
		
		return new ModelAndView("del.json",mmap);
	}
	
	private void checkBean(MenuCategoriesBeanDto mcbd,ModelMap mmap){
		
		try {
			
			_validateForm.hasError(mcbd);
			
			mmap.put("code", "0");
			
		} catch (MenuCategoriesException e) {
			// TODO: handle exception
			
			mmap.put("code", e.getErrorCode());
			
			mmap.put("info", e.getMessage());
			
			
			
		}catch (Exception e){
			
			mmap.put("code", "-1");
			
			mmap.put("info", e.getMessage());
			
			
			
		}
		
	}
	
	private void showRquestProperty(HttpServletRequest request){
		
		 Enumeration paramNames = request.getParameterNames();  
	      while (paramNames.hasMoreElements()) {  
	       String paramName = (String) paramNames.nextElement();  
	   
	          String[] paramValues = request.getParameterValues(paramName);  
	       if (paramValues.length == 1) {  
	        String paramValue = paramValues[0];  
	          if (paramValue.length() != 0) {  
	           System.out.println("参数：" + paramName + "=" + paramValue);  
	            
	         }  
	       }  
	   }  
		
	}
	
	
	
	
}
